﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DispatchAssist.Weather.Interfaces;
using DispatchAssist.Weather.Entities;

namespace DispatchAssist.Weather.Controls
{
    /// <summary>
    /// 天气图树节点
    /// </summary>
    /// <remarks>
    /// 只允许内部新增和删除
    /// </remarks>
    class WeatherMapTypeTreeNode : TreeNode
    {
        public string TypeName { get; set; }
        public IWeatherMapType MapType { get; private set; }
        #region 构造

        public WeatherMapTypeTreeNode(IWeatherMapType mapType)
            : base()
        {
            if (mapType != null)
            {
                this.Text = mapType.Name;
                MapType = mapType;
            }
        }


        #endregion
        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private void AddChild(IWeatherMapInfo info)
        {
            if (info == null)
                throw new ArgumentNullException("type");

            this.Nodes.Add(new WeatherMapTreeNode(info.Name, info));

        }
        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <param name="infos"></param>
        private void AddChilds(IList<IWeatherMapInfo> infos)
        {
            foreach (var info in infos)
            {
                AddChild(info);
            }
        }
        /// <summary>
        /// 删除子节点
        /// </summary>
        /// <param name="type"></param>
        internal void DeleteChild(IWeatherMapInfo info)
        {
            if (info == null)
                throw new ArgumentNullException("info");


            foreach (WeatherMapTreeNode node in this.Nodes)
            {
                if (node == null)
                    continue;

                if (node.MapInfo == info)
                {
                    this.Nodes.Remove(node);
                }
            }
        }

        /// <summary>
        /// 递归添加子类型和天气图信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private void AddChildTypes(IWeatherMapType type)
        {
            if (type == null)
                return;

            var typeNode = new WeatherMapTypeTreeNode(type);
            this.Nodes.Add(typeNode);

            typeNode.AddChilds(type.Infos);

            foreach (var t in type.ChildTypes)
            {
                typeNode.AddChildTypes(t);
            }
        }

        public static void AddChildTypes(TreeView tv, IWeatherMapType type)
        {
            if (type == null)
                return;

            var typeNode = new WeatherMapTypeTreeNode(type);
            tv.Nodes.Add(typeNode);

            typeNode.AddChilds(type.Infos);

            foreach (var t in type.ChildTypes)
            {
                typeNode.AddChildTypes(t);
            }
        }
        /// <summary>
        /// 删除子类型
        /// </summary>
        /// <param name="type"></param>
        internal void DeleteChildType(IWeatherMapType type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            foreach (WeatherMapTypeTreeNode node in this.Nodes)
            {
                if (node == null)
                    continue;

                if (node.MapType == type)
                {
                    this.Nodes.Remove(node);
                }
            }
        }
    }
}
